package br.game;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

public class MainShipAsteroid extends Applet implements GameLoop, Runnable {

	private Image asteroidImage;
	private Image ship;
	private Graphics bufferGraphics;
	private Image offscreen;
	private Thread timer;
	private int x = 0, y = 0;
	int lugar = (int) (Math.random() * 800);
	int lugar2 = (int) (Math.random() * 800);
	int lugar3 = (int) (Math.random() * 800);
	int lugar4 = (int) (Math.random() * 800);

	public void logicGame() {
		x += 10;
		y += 8;

	}

	public void drawGame() {
		repaint();
	}

	public void score() {

	}

	public void run() {
		while (true) {
			logicGame();
			drawGame();
			sleepGame(50);
		}
	}

	public void sleepGame(int mili) {
		try {
			Thread.sleep(mili);

		} catch (InterruptedException e) {
		}

	}

	public void init() {
		x = 0;
		setBackground(Color.BLACK);
		setSize(800, 600);
		Toolkit drawImage = Toolkit.getDefaultToolkit();
		asteroidImage = drawImage
				.getImage("../Sprites- Imagens/Asteroide05.png");

		ship = drawImage
				.getImage("../Sprites- Imagens/Nave.png");

		offscreen = createImage(getSize().width, getSize().height);
		bufferGraphics = offscreen.getGraphics();
		timer = new Thread(this);
		timer.start();
	}

	public void paint(Graphics g) {

		bufferGraphics.clearRect(0, 0, getSize().width, getSize().height);
		if (x <= 900) {
			bufferGraphics.drawImage(asteroidImage, 750 - x, lugar, this);
			bufferGraphics.drawImage(asteroidImage, 900 - x, lugar2, this);

		} else {
			lugar = (int) (Math.random() * 590);
			lugar2 = (int) (Math.random() * 590);
			x = 0;
		}

		if (y <= 950) {
			bufferGraphics.drawImage(asteroidImage, 800 - y, lugar3, this);
			bufferGraphics.drawImage(asteroidImage, 950 - y, lugar4, this);

		} else {
			lugar3 = (int) (Math.random() * 590);
			lugar4 = (int) (Math.random() * 590);
			y = 0;
		}

		bufferGraphics.drawImage(ship, 0, 200, this);

		g.drawImage(offscreen, 0, 0, this);
	}

	public void update(Graphics g) {
		paint(g);
	}

}
